﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Company.Shell.Controls
{
    /// <summary>
    /// Engine.xaml 的交互逻辑 - 发动机控件
    /// </summary>
    public partial class Engine : UserControl
    {
        private DoubleAnimation fanRotationAnimation;
        private DoubleAnimation pistonAnimation;

        public Engine()
        {
            InitializeComponent();
            InitializeAnimations();
        }

        /// <summary>
        /// 发动机状态：0=停止，1=运行，2=故障
        /// </summary>
        public int State
        {
            get { return (int)GetValue(StateProperty); }
            set { SetValue(StateProperty, value); }
        }

        public static readonly DependencyProperty StateProperty =
            DependencyProperty.Register("State", typeof(int), typeof(Engine),
                new PropertyMetadata(0, new PropertyChangedCallback(StateChangedCallback)));

        private static void StateChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Engine)d;
            if (control == null) return;

            if (e.NewValue is int state)
            {
                control.UpdateEngineState(state);
            }
        }

        /// <summary>
        /// 压力值 (PSI)
        /// </summary>
        public int Pressure
        {
            get { return (int)GetValue(PressureProperty); }
            set { SetValue(PressureProperty, value); }
        }

        public static readonly DependencyProperty PressureProperty =
            DependencyProperty.Register("Pressure", typeof(int), typeof(Engine),
                new PropertyMetadata(0, new PropertyChangedCallback(PressureChangedCallback)));

        private static void PressureChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Engine)d;
            if (control == null) return;

            if (e.NewValue is int pressure)
            {
                control._pressureText.Text = $"{pressure} PSI";
               
            }
        }

        /// <summary>
        /// 转速 (RPM)
        /// </summary>
        public int RPM
        {
            get { return (int)GetValue(RPMProperty); }
            set { SetValue(RPMProperty, value); }
        }

        public static readonly DependencyProperty RPMProperty =
            DependencyProperty.Register("RPM", typeof(int), typeof(Engine),
                new PropertyMetadata(0, new PropertyChangedCallback(RPMChangedCallback)));

        private static void RPMChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Engine)d;
            if (control == null) return;

            if (e.NewValue is int rpm)
            {
                control._rpmText.Text = $"{rpm} RPM";
                control.UpdateFanSpeed(rpm);
            }
        }

        /// <summary>
        /// 温度状态：0=正常，1=高温警告
        /// </summary>
        public int TemperatureState
        {
            get { return (int)GetValue(TemperatureStateProperty); }
            set { SetValue(TemperatureStateProperty, value); }
        }

        public static readonly DependencyProperty TemperatureStateProperty =
            DependencyProperty.Register("TemperatureState", typeof(int), typeof(Engine),
                new PropertyMetadata(0, new PropertyChangedCallback(TemperatureStateChangedCallback)));

        private static void TemperatureStateChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Engine)d;
            if (control == null) return;

            if (e.NewValue is int tempState)
            {
                control.UpdateTemperatureIndicator(tempState);
            }
        }

        private void UpdateEngineState(int state)
        {
            switch (state)
            {
                case 0: // 停止状态
                    _runIndicator.Background = new SolidColorBrush(Color.FromRgb(128, 128, 128));
                    _faultIndicator.Background = new SolidColorBrush(Color.FromRgb(128, 128, 128));
                    _statusText.Text = "停止";
                    _statusText.Foreground = new SolidColorBrush(Color.FromRgb(255, 64, 64));
                    _runGlow.Color = Colors.Gray;
                    _runGlow.Opacity = 0.3;
                    _faultGlow.Opacity = 0;
                    StopAnimations();
                    break;

                case 1: // 运行状态
                    _runIndicator.Background = new SolidColorBrush(Color.FromRgb(64, 255, 64));
                    _faultIndicator.Background = new SolidColorBrush(Color.FromRgb(128, 128, 128));
                    _statusText.Text = "运行";
                    _statusText.Foreground = new SolidColorBrush(Color.FromRgb(64, 255, 64));
                    _runGlow.Color = Colors.LimeGreen;
                    _runGlow.Opacity = 0.8;
                    _faultGlow.Opacity = 0;
                    StartAnimations();
                    break;

                case 2: // 故障状态
                    _runIndicator.Background = new SolidColorBrush(Color.FromRgb(128, 128, 128));
                    _faultIndicator.Background = new SolidColorBrush(Color.FromRgb(255, 64, 64));
                    _statusText.Text = "故障";
                    _statusText.Foreground = new SolidColorBrush(Color.FromRgb(255, 64, 64));
                    _runGlow.Color = Colors.Gray;
                    _runGlow.Opacity = 0.3;
                    _faultGlow.Color = Colors.Red;
                    _faultGlow.Opacity = 0.8;
                    StopAnimations();
                    break;
            }
        }

        private void UpdateTemperatureIndicator(int tempState)
        {
            if (tempState == 1) // 高温警告
            {
                _tempIndicator.Background = new SolidColorBrush(Color.FromRgb(255, 165, 0));
                _tempGlow.Color = Colors.Orange;
                _tempGlow.Opacity = 0.8;
            }
            else // 正常温度
            {
                _tempIndicator.Background = new SolidColorBrush(Color.FromRgb(64, 255, 64));
                _tempGlow.Color = Colors.LimeGreen;
                _tempGlow.Opacity = 0.5;
            }
        }

       

        private void UpdateFanSpeed(int rpm)
        {
            if (rpm > 0 && State == 1) // 运行状态且有转速
            {
                // 根据转速调整风扇旋转速度
                double duration = Math.Max(0.1, 2.0 - (rpm / 2000.0) * 1.8);
                fanRotationAnimation.Duration = TimeSpan.FromSeconds(duration);
                _fanRotateTransform.BeginAnimation(RotateTransform.AngleProperty, fanRotationAnimation);
            }
        }

        private void InitializeAnimations()
        {
            // 风扇旋转动画
            fanRotationAnimation = new DoubleAnimation()
            {
                From = 0,
                To = 360,
                Duration = TimeSpan.FromSeconds(2),
                RepeatBehavior = RepeatBehavior.Forever
            };

            // 活塞运动动画
            pistonAnimation = new DoubleAnimation()
            {
                From = 0,
                To = 20,
                Duration = TimeSpan.FromSeconds(0.8),
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever
            };
        }

        private void StartAnimations()
        {
            if (RPM > 0)
            {
                _fanRotateTransform.BeginAnimation(RotateTransform.AngleProperty, fanRotationAnimation);
            }
            
            // 启动活塞动画
            var translateTransform = new TranslateTransform();
            _pistonHead.RenderTransform = translateTransform;
            translateTransform.BeginAnimation(TranslateTransform.YProperty, pistonAnimation);
        }

        private void StopAnimations()
        {
            _fanRotateTransform.BeginAnimation(RotateTransform.AngleProperty, null);
            _fanRotateTransform.Angle = 0;
            
            if (_pistonHead.RenderTransform is TranslateTransform transform)
            {
                transform.BeginAnimation(TranslateTransform.YProperty, null);
            }
        }
    }
}
